home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 26 / AMIGAplus Sonderheft 26 (2000)(Falke)(DE)(Track 1 of 2)[!].iso / Tools / GFX-Viewer / Ham32k / Ham32k.c < prev    next >
C/C++ Source or Header  |  1999-03-29  |  5KB  |  156 lines

  1. /*        Image Enhancement Programme    */
  2. /*        S.D.Price             */
  3. /*        July 98                */
  4. /*        Compiled using North C          */
  5. /*        By Steve Hawtin                 */
  6.  
  7.  
  8. #include <stdio.h>
  9.  
  10. #define MAXWIDTH     1024
  11.  
  12. main(argc,argv)
  13. int     argc;
  14. char     *argv[];
  15.  
  16. {    
  17.  
  18.  
  19.     FILE     *fpsrc,*fpout;
  20.     float     const;
  21.     short     count=0,count2=0,width,height,oldheight,pad,loop,count3;
  22.     unsigned char    input,char1=0,widthlsb,widthmsb,heightlsb,heightmsb,outnme[32];
  23.     unsigned char    value,tempval[2];
  24.     unsigned char    botval[3][MAXWIDTH],mark,mark2;
  25.  
  26. /* ----------- Get command line args and check for problems, open files. ----------------- */
  27.  
  28.  
  29.     if(argc!=2&&argc!=3)
  30.     {
  31.         puts("\nFormat:\nHam32k <Inputfile> [Outputfile]\n\n");
  32.         exit(1);
  33.     }
  34.         
  35.     fpsrc=fopen(argv[1],"r");
  36.     if(fpsrc==(FILE *)NULL)
  37.     {
  38.         puts("\nCant find input file.\n");
  39.         exit(1);
  40.     }
  41.     
  42.     if(argc==2){
  43.         strcpy(outnme,argv[1]);
  44.         strcat(outnme,".32k");}
  45.     else{
  46.         strcpy(outnme,argv[2]);}
  47.  
  48.     fpout=fopen(outnme,"r");
  49.     if(fpout!=(FILE *)NULL)
  50.     {
  51.         puts("\nOutput file already exists!\n");
  52.         exit(1);
  53.     }
  54.     fpout=fopen(outnme,"w");
  55.     if(fpout==(FILE *)NULL)
  56.     {
  57.         puts("\nI cant open the output file!\n");
  58.         exit(1);
  59.     }
  60.     
  61.  
  62. /*-------------------------- Begining of main part of program. ------------------------------------*/
  63.  
  64.     puts("\nImage enhancement programme,\nBy SDPrice 1998\n");  /* Prog start banner */
  65.  
  66.     for(count=0;count!=18;count++)
  67.     {    char1=fgetc(fpsrc);         /* Read in and output first part of file header.   */
  68.         fputc(char1,fpout);}
  69.     
  70.     widthlsb=fgetc(fpsrc);               /* Read in width LeastSignificantByte  */
  71.     widthmsb=fgetc(fpsrc);               /* Read in width MostSB                */
  72.     width=widthlsb+widthmsb*256;         /* Calculate value of width            */
  73.     if(width>MAXWIDTH){
  74.         printf("\nSorry this release can only handle files up to %d wide.",MAXWIDTH);
  75.         exit(1);}
  76.  
  77.     fputc(widthlsb,fpout);            /* Output the width value to           */
  78.     fputc(widthmsb,fpout);            /* File header of the output file.     */
  79.  
  80.     char1=fgetc(fpsrc);         /* Read in and                            */
  81.     fputc(char1,fpout);         /* Output the next 2 bytes of the header. */
  82.     char1=fgetc(fpsrc); 
  83.     fputc(char1,fpout);
  84.  
  85.     heightlsb=fgetc(fpsrc);     /* Now read in and convert the height value.   */
  86.     heightmsb=fgetc(fpsrc); 
  87.  
  88.     height=(heightlsb+heightmsb*256)*2;  /* Times the height value by 2 before outputting */
  89.     oldheight=height/2;                  /* Added later to speed up loop.      */
  90.     heightmsb=height/256;                /* Reconvert to LSB and MSB   before             */
  91.     heightlsb=height%256;
  92.     fputc(heightlsb,fpout);              /* Outputting to the out file.                   */
  93.     fputc(heightmsb,fpout);
  94.     printf("Output will be called %s\n",outnme);
  95.     printf("BMP file.       Original dimensions: %d X %d\n",width,oldheight);   
  96.     printf("                New dimensions: %d X %d\n",width,height);
  97.     puts("File conversion will take some time.\nPercent complete =   0%\b\b\b\b");
  98.     
  99.     pad=4-((width*3)%4);         /* Calculate no. of padding 0's per line required.  */
  100.     pad=pad&3;
  101.  
  102.     /*  Now at pos 24, data starts at 109.   Read in and output rest of header.  */
  103.     for(count=24;count<54;++count)
  104.     {    char1=fgetc(fpsrc);         /* Read in and output last part of file header.   */
  105.         fputc(char1,fpout);
  106.     }
  107.     
  108. /* ---------------------------------- Begin processing. ----------------------------------------- */
  109.  
  110. for(loop=0;loop<oldheight;++loop)   /*  Loop for all lines.   */
  111. {
  112.     mark=0;
  113.     mark2=1;
  114.     for(count=0;count<width;++count)   /*  Repeat for 1 whole line   */
  115.     {
  116.         for(count2=0;count2<3;++count2)    /* Loop 3 times for R G B   */ 
  117.         {
  118.             value=fgetc(fpsrc);        /* Read in the R G B values. */
  119.             tempval[mark]=value&240;        /* Split into top and bottom pixels */
  120.             tempval[mark2]=(((value&8) && value<240) ? tempval[mark]+16 : tempval[mark]); 
  121.             fputc(tempval[0],fpout);                /* Output top row pixel. */
  122.             botval[count2][count]=tempval[1];
  123.         }
  124.         mark=!mark;
  125.         mark2=!mark2;
  126.     }
  127. /*  Deal with the padding at the end of each line.   */
  128.     for(count3=0;count3<pad;++count3){
  129.         char1=fgetc(fpsrc);
  130.         if(char1!=0){
  131.             puts("\nBug in program!\n\n");  /* Padding is always a zero! */
  132.             exit(1);}
  133.         fputc(0,fpout);
  134.     }
  135. /* Write out the bottom row of pixels  */
  136.     for(count=0;count<width;++count){           /* Loop till end of line.  */
  137.         fputc(botval[0][count],fpout);      /* Output Red,     */
  138.         fputc(botval[1][count],fpout);      /* Green,          */
  139.         fputc(botval[2][count],fpout);      /* and Blue pixels */
  140.     }
  141.     for(count3=0;count3<pad;++count3)    /* Finaly write out the padding  */
  142.         fputc(0,fpout);    
  143.     
  144.     const=(100.0/(float)oldheight)*((float)loop+1);
  145.     printf("%3d\b\b\b",(unsigned)const);  /* Completion report. */
  146.     fflush(stdout);
  147. }
  148.  
  149.     fclose(fpout);
  150.     fclose(fpsrc);
  151.     
  152.     puts("\nAll finnished!\n\n");
  153. }
  154.  
  155.  
  156.